package day12.charstream;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

/**
 * @author Xiao
 * @date 2021/8/1
 */
public class CharStreamDemo01 {
    /**
     * 	编码：
     * 	byte[] getBytes()：使用平台的默认字符集将该 String编码为一系列字节，将结果存储到新的字节数组中
     * 	byte[] getBytes(String charsetName)：使用指定的字符集将该 String编码为一系列字节，将结果存储到新的字节数组中
     * 	解码：
     * 	String(byte[] bytes)：通过使用平台的默认字符集解码指定的字节数组来构造新的 String
     * 	String(byte[] bytes, String charsetName)：通过指定的字符集解码指定的字节数组来构造新的 String
     */
    public static void main(String[] args) throws UnsupportedEncodingException {
        method1();
        method2();
    }

    private static void method2() throws UnsupportedEncodingException {
        byte[] bytes1 = {-26, -120, -111, -25, -120, -79, -28, -67, -96};
        byte[] bytes2 = {-50, -46, -80, -82, -60, -29};
        String s1 = new String(bytes1);
        String s2 = new String(bytes2, "gbk");
        System.out.println(s1);
        System.out.println(s2);
    }

    private static void method1() throws UnsupportedEncodingException {
        String s = "我爱你";
        byte[] bytes1 = s.getBytes();
        byte[] bytes2 = s.getBytes("UTF-8");
        byte[] bytes3 = s.getBytes("GBK");
        System.out.println(Arrays.toString(bytes1));
        System.out.println(Arrays.toString(bytes2));
        System.out.println(Arrays.toString(bytes3));
    }
}
